package cn.doctor.assistant.pad.ui.ziliao;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.doctor.assistant.pad.R;
import cn.doctor.assistant.pad.api.ziliao.AddPatientRequest;
import cn.doctor.assistant.pad.api.ziliao.InformatApi;
import cn.doctor.assistant.pad.base.BaseNetActivity;
import cn.doctor.assistant.pad.base.ResultCallback;
import cn.doctor.assistant.pad.ui.SpinnerHelper;
import cn.doctor.assistant.pad.ui.UI;

/**
 * Created by jiaolf on 2016/3/31.
 */
public class AddPatientActivity extends BaseNetActivity {

    @InjectView(R.id.titlebar_left_title)
    TextView title;
    @InjectView(R.id.titlebar_right_text)
    TextView rightText;
    @InjectView(R.id.icon)
    ImageView icon;
    @InjectView(R.id.edit_name)
    EditText editName;
    @InjectView(R.id.edit_phone)
    EditText editPhone;
    @InjectView(R.id.spinner_marriage)
    Spinner spinnerMarriage;
    @InjectView(R.id.spinner_sex)
    Spinner spinnerSex;
    @InjectView(R.id.edit_birthday)
    EditText editBirthday;
    @InjectView(R.id.edit_address)
    EditText editAddress;

    String[] items_marriage = {"未知", "未婚", "已婚"};
    String[] items_sex = {"未知", "男", "女"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_patient);
        ButterKnife.inject(this);
        initialize();
    }

    void initialize() {
        SpinnerHelper.init(spinnerMarriage, items_marriage, new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        SpinnerHelper.init(spinnerSex, items_sex, new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }

    @OnClick({R.id.titlebar_left_title, R.id.titlebar_right_text, R.id.edit_birthday})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.titlebar_left_title:
                back();
                break;
            case R.id.titlebar_right_text:
                save();
                break;
            case R.id.edit_birthday:
                setBirthday();
                break;
        }
    }

    void save() {
        AddPatientRequest patient = new AddPatientRequest();

        String name = editName.getText().toString();
        String phone = editPhone.getText().toString();
        int marriary = spinnerMarriage.getSelectedItemPosition();
        int sex = spinnerSex.getSelectedItemPosition();
        String address = editAddress.getText().toString();

        if (TextUtils.isEmpty(name)) {
            showToast("名字不能为空");
            return;
        }

        patient.setName(name);
        patient.setPhone(phone);
        patient.setMarriage(marriary);
        patient.setSex(String.valueOf(sex));
        patient.setBirthday(birthdayDate);
        patient.setAddress(address);

        showProgress("正在保存...");
        InformatApi.addPatient(patient, new ResultCallback<String>() {
            @Override
            public void onSuccess(String result) {
                stopProgress();
                UI.showToast("保存成功");
                back();
            }
        });
    }

    String birthday;
    String birthdayDate; // 格式 "Apr 1, 2016 3:01:24 PM"
    void setBirthday() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int monthOfYear = calendar.get(Calendar.MONTH);
        int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH) + 1;

        new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
                birthday = String.format("%s-%02d-%02d", year, monthOfYear + 1, dayOfMonth);
                editBirthday.setText(birthday);

                Date date = new Date();
                date.setYear(year - 1900);
                date.setMonth(monthOfYear);
                date.setDate(dayOfMonth);

                birthdayDate = String.format(Locale.US, "%tb %d, %d 00:00:00", date, dayOfMonth, year);
                UI.showToast(birthdayDate);

            }

        }, year, monthOfYear, dayOfMonth).show();
    }
}
